package biz.bbtec.sbm.client.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import biz.bbtec.sbm.client.bean.BusinessBean;
import biz.bbtec.sbm.client.bean.ClientBean;
import biz.bbtec.sbm.client.bean.NoteBean;
import biz.bbtec.sbm.client.factory.NoteFactory;
import biz.bbtec.sbm.client.service.NoteService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class NoteAction extends ActionSupport implements SessionAware {
	private NoteBean note = new NoteBean();
	private List<NoteBean> notelist = new ArrayList<NoteBean>();
	private Map session;
	private BusinessBean business;
	
	private File image;
	private String imageFileName;
	private String imageFileContent;
	private int pageNow;
	public int getPageNow(){
		return this.pageNow;
	}
	public void setPageNow(int pageNow){
		this.pageNow=pageNow;
	}
	
	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageFileContent() {
		return imageFileContent;
	}

	public void setImageFileContent(String imageFileContent) {
		this.imageFileContent = imageFileContent;
	}

	public BusinessBean getBusiness() {
		return business;
	}

	public void setBusiness(BusinessBean business) {
		this.business = business;
	}

	public List<NoteBean> getNotelist() {
		return notelist;
	}

	public void setNotelist(List<NoteBean> notelist) {
		this.notelist = notelist;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public NoteBean getNote() {
		return note;
	}

	public void setNote(NoteBean note) {
		this.note = note;
	}

	public String modifyNote() throws Exception {
		if (note.getTitle().trim().equalsIgnoreCase("")) {
			this.addActionError("留言名称不能为空！");
			return INPUT;
		}
		if (note.getText().trim().equalsIgnoreCase("")) {
			this.addActionError("留言内容不能为空！");
			return INPUT;
		}
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		if (!noteservice.ModifyNote(note)) {
			this.addActionError("数据库操作失败！");
			return INPUT;
		}
		Map session = ActionContext.getContext().getSession();
		session.remove("note");
		return SUCCESS;
	}

	public String deleteNote() throws Exception {
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		if (!noteservice.DeleteNote(note)) {
			this.addActionError("未知错误！");
			return INPUT;
		} else {
			this.addActionError("删除成功！");
			return SUCCESS;
		}
	}

	public String addNote() throws Exception {
		if(image != null){
			Random rnd = new Random();
			int p = rnd.nextInt(20);
			String num = Integer.toString(p);

			String path = ServletActionContext.getServletContext().getRealPath(
					"/images");

			imageFileName = num + "." + imageFileName;

			File file = new File(path + "//" + imageFileName);
			FileUtils.copyFile(image, file);
		} else{
			imageFileName = null;
		}
			
		if (note.getTitle().trim().equalsIgnoreCase("")) {
			this.addActionError("留言名称不能为空！");
			return INPUT;
		}
		if (note.getText().trim().equalsIgnoreCase("")) {
			this.addActionError("留言内容不能为空！");
			return INPUT;
		}
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		if (!noteservice.AddNote(note,imageFileName)) {
			this.addActionError("数据库操作失败！");
			return INPUT;
		} else {
			this.addActionError("添加成功！");
			return SUCCESS;
		}
	}

	public String showallNote() throws Exception {
		Map session = ActionContext.getContext().getSession();
		System.out.println("called");
		if(session.get("clientid")==null){
			ClientBean client = new ClientBean();
			int clientid = (Integer) session.get("clientid");
			client.setClientid(clientid);
			System.out.println("called1");
		}
		if(note.getBizid() != 0){
			business = new BusinessBean();
			business.setBizid(note.getBizid());
		}
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		notelist = noteservice.ShowAllNote(business,getPageNow(),15);
		if (notelist.size() == 0) {
			this.addActionError("没有笔记！");
			this.session.put("bizid", business.getBizid());
			return INPUT;
		} else {
			int notebizid = notelist.get(0).getBizid();
			this.session.put("bizid", notebizid);
			return SUCCESS;
		}
	}
	
	public String showoneNote() throws Exception {
		business = new BusinessBean();
		business.setBizid(note.getBizid());
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		note = noteservice.ShowOneNote(note);
		if (note == null) {
			this.addActionError("Error!");
			return INPUT;
		} else {
			this.session.put("note", note);
			return SUCCESS;
		}
	}
}
